regexify_names <- function(x, method='strict'){
  frac <- unlist(strsplit(x, '\\s'))
  if(any(grepl('(^(v|V)on$)|(^(d|D)(e(s)?|i)$)', frac))) frac <- c(paste(frac[1], frac[2]), frac[3:length(frac)])
  if(any(grepl('[a-z]\\-[A-Z]', frac))){
    for(i in frac[grepl('\\-', frac)]){
      frac[frac==i] <- paste0(
        '((', gsub('(.*[A-z]+\\-)', '(\\1)?', i),')|(', gsub('(\\-[A-z].*)', '(\\1)?', i),'))'
      )
    }
  }
  if(length(frac)==2){
    frac <- paste(frac[2], frac[1])
  }else{
    if(grepl('\\(.*\\)', frac[length(frac)])){
      frac[length(frac)] <- gsub('([[:punct:]])', '(\\\\\\1)?', frac[length(frac)])
      frac <- paste0(frac[2], # formerly: frac[length(frac)-1]
                     paste0('((\\s)?(',frac[-c(1:2)],')?(\\s)?)', collapse = '(\\s)?'), 
                     # formerly: paste0('((\\s)?(',frac[-c(1, length(frac))],')?(\\s)?)', collapse = '(\\s)?')
                     frac[1]
      )
    }else{
      if(method=='loose'){
        frac.var <- paste0(
          frac[length(frac)],
          #paste0('((\\s)?(',frac[-c(1:2)],')?(\\s)?)', collapse = '(\\s)?'), 
          paste0(
            '(\\s', frac[1],
            '|',
            paste0('((\\s)?(',frac[-c(1,length(frac))],')(\\s)?)', collapse = '(\\s)?'),
            ')'
          )
        )
        frac <- paste0(frac[2], # formerly: frac[length(frac)]
                       paste0('((\\s)?(',frac[-c(1:2)],')?(\\s)?)', collapse = '(\\s)?'), 
                       # paste0('((\\s)?(',frac[-c(1,length(frac))],')?(\\s)?)', collapse = '(\\s)?')
                       frac[1]
        )
        frac <- paste0('(', paste0('(', frac, ')|(', frac.var, ')'), ')')
      }else{
        frac <- paste0(frac[2], # formerly: frac[length(frac)]
                       paste0('((\\s)?(',frac[-c(1:2)],')?(\\s)?)', collapse = '(\\s)?'), 
                       # paste0('((\\s)?(',frac[-c(1,length(frac))],')?(\\s)?)', collapse = '(\\s)?')
                       frac[1]
        )
      }
    }
    
  }
  return(paste0('(', gsub(' ', '\\\\s', frac), ')'))
}



############## dev
if(F){
  regexify_names2 <- function(firstN, lastN,  method='strict', lastN.first=F){
    if(grepl('[a-z](\\-|\\s)[A-Z]', lastN)){
      if(grepl('[a-z](\\-)[A-Z]', lastN)){
        lastN <- 
          paste0(
            '((', gsub('(.*[A-z]+\\-)', '(\\1)?', lastN),')|(', gsub('(\\-[A-z].*)', '(\\1)?', lastN),'))'
          )
      }
      if(grepl('[a-z](\\s)[A-Z]', lastN)){
        lastN <- 
          paste0(
            '((', gsub('(.*[A-z]+)\\s', '(\\1\\\\s)?', lastN),')|(', gsub('\\s([A-z].*)', '(\\\\s\\1)?', lastN),'))'
          )
      }
      
    }
    if(length(unlist(strsplit(firstN, '\\s')))>1){
      firstN <- unlist(strsplit(firstN, '\\s'))
      firstN <-
        paste0(
          firstN[1],
          paste0(
            '((\\s)?(',firstN[2:length(firstN)],')?(\\s)?)', collapse = '(\\s)?'
          )
        )
    }else{
      firstN <- paste0(firstN, '\\s')
    }
    if(lastN.first) return(paste0(lastN, firstN))
    return(paste0(firstN, lastN))
  }
}


regexify_names2 <- function(firstN, lastN,  method='strict', lastN.first=F){
    # handler function
    mod <- function(prefix=NULL, x, suffix=NULL) paste0('(', prefix, x, suffix, ')?')
    word <- function(x) paste0('\\b', x, '\\b')
    spacer <- function(x, head=T, tail=T){
      if(!head & tail){
        paste0(as.vector(sapply(unlist(strsplit(x, '\\s')), function(y) paste0(y, '(\\s)?'))), collapse='')
      }else if(head & !tail){
        paste0(as.vector(sapply(unlist(strsplit(x, '\\s')), function(y) ifelse(!y==unlist(strsplit(x, '\\s'))[length(unlist(strsplit(x, '\\s')))], paste0(y, '(\\s)?'), y))), collapse='')
      }else if(head & tail){
        paste0(as.vector(sapply(unlist(strsplit(x, '\\s')), function(y) ifelse(!y==unlist(strsplit(x, '\\s'))[1], paste0(y, '(\\s)?'), paste0('(\\s)?', y, '(\\s)?')))), collapse='')
      }
    }
    tailEnds <- function(...) paste0('((\\s)?', ..., '(\\s)?)')
    
    ## start of function
    if(length(unlist(strsplit(lastN, '\\s|\\-')))<3){ # for the case that people less than 3 lastnames (normal case)
      if(grepl('[a-z](\\-|\\s)[A-Z]', lastN)){
        if(grepl('[a-z](\\-)[A-Z]', lastN)){
          lastN <- 
            paste0(
              '((', gsub('(.*[A-z]+\\-)', '(\\1)?', lastN),')|(', gsub('(\\-[A-z].*)', '(\\1)?', lastN),'))'
            )
        }
        if(grepl('[a-z](\\s)[A-Z]', lastN)){
          lastN <- 
            paste0(
              '((', gsub('(.*[A-z]+)\\s', '(\\1\\\\s)?', lastN),')|(', gsub('\\s([A-z].*)', '(\\\\s\\1)?', lastN),'))'
            )
        }
        
      }
    }else{ # for the case that people have 3 or more lastnames
      #lastN <- "von Sury d'Aspremont-Heidegger"
      if(grepl('(\\b(v|V)on\\b)|(\\b(d|D)(e(s)?|i)\\b)', lastN)){
        if(grepl('[A-z]+\\-[A-z]+', lastN)){
          lnSplit <- unlist(strsplit(lastN, '\\-'))
          lastN <- tailEnds(spacer(lnSplit[1], tail=F), mod('\\-', lnSplit[2]))
        }else{
          lastN <- tailEnds(spacer(lastN))
        }
      }else{
        lastN <- tailEnds(spacer(lastN))
      }
    }
    
    if(length(unlist(strsplit(firstN, '\\s')))>1){
      firstN <- unlist(strsplit(firstN, '\\s'))
      firstN <-
        paste0(
          firstN[1],
          paste0(
            '((\\s)?(',firstN[2:length(firstN)],')?(\\s)?)', collapse = '(\\s)?'
          )
        )
    }else{
      firstN <- paste0(firstN, '\\s')
    }
    
    if(lastN.first) return(paste0(lastN, firstN))
    return(paste0(firstN, lastN))
}

#regexify_names2(firstN='Heiri', lastN='Müller Hans')
#"(?<=[\b(v|V)on\b|\b(d|D)(e(s)?|i|\')\b)])(\s)?\b[A-z]+\b"
#regexify_names2(firstN='Heiri', lastN="von Sury d'Aspremont-Heidegger")
#regexify_names2(firstN='Heiri', lastN="Hänni Holli Heidegger")
  